package com.mwh.yuantu.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mwh.yuantu.entity.TypeFieldEntity;
import com.mwh.yuantu.parameter.TypeFieldParameter;
import com.mwh.yuantu.parameter.TypeFieldSearchParameter;
import com.mwh.yuantu.dto.TypeFieldDTO;
import org.springframework.beans.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * (TypeField)表服务接口
 *
 * @author maweihong
 * @since 2023-11-01 09:47:32
 */
@Service
@Slf4j
public class TypeFieldService extends BaseService {



    /**
     * 根据分类信息主键，查询该分类下的字段配置
     * @param typeId 分类信息主键
     * @return
     */
    public List<TypeFieldEntity> listByTypeId(String typeId) {
        return iTypeFieldDAO.listByTypeId(typeId);
    }

    /**
     * 保存某个分类信息下的字段配置
     * @param typeId 分类信息主键
     * @param typeFieldList
     * @param fieldIdArr 字段主键数组
     */
    public void save(String typeId, List<TypeFieldEntity> typeFieldList, String[] fieldIdArr) {
        // 1.0 删除该分类下的所有字段配置
        iTypeFieldDAO.deleteByTypeId(typeId);

        // 2.0 批量插入分类信息下的字段配置
        iTypeFieldDAO.batchInsert(typeFieldList);

        // 3.0 生成关联字段
        // 3.1 获取字段的变量名集合
        List<String> list = iFieldInfoDAO.listVarNameByIdArr(fieldIdArr);
        if (list!=null && list.isEmpty()==false) {
            int len = list.size();
            for (int i=0; i<len; i++) {
                String varName = list.get(i);

                StringBuffer sql = new StringBuffer();
                sql.append(" IF NOT EXISTS ( ");
                sql.append(" SELECT ");
                sql.append(" * ");
                sql.append(" FROM ");
                sql.append(" syscolumns ");
                sql.append(" WHERE ");
                sql.append(" id = object_id('media_info') ");
                sql.append(" AND name = '"+varName+"' ");
                sql.append(" ) ");
                sql.append(" BEGIN ");
                sql.append(" ALTER TABLE media_info ADD "+varName+" VARCHAR (100) ");
                sql.append(" END ");

                iMediaInfoDAO.alter(sql.toString());
            }
        }
    }

    /**
     * 检索指定字段是否必填
     * @param typeId 分类信息主键
     * @param varName 字段变量名
     * @return
     */
    public String selectIsRequired(String typeId, String varName) {
        return iTypeFieldDAO.selectIsRequired(typeId, varName);
    }

}

